package com.wx.controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 服务信息列表展示
 *
 */
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.pojo.Category;
import com.wx.pojo.CategorySourceVO;
import com.wx.pojo.IndexSort;
import com.wx.pojo.IndexSource;
import com.wx.pojo.Source;
import com.wx.service.AssortmentService;
import com.wx.service.SourceService;
/**
 *商品信息
 *首页商品列表展示 
 *
 */
@Controller
@RequestMapping("/source")
public class SourceController {

	@Autowired
	private SourceService sourceService;
	
	@Autowired
	private AssortmentService assortmentService;
	/**
	 * 查询全部
	 * @return
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> SourceIndexList( ){
		
		Map<String,Object> modelMap= new HashMap<String,Object>();
		List<IndexSort> indexSortList=new ArrayList<>();
		IndexSort sourceAll=findSourceAll();
		IndexSort sourceRecent=findSourceRecent();
		IndexSort sourceTop=findSourceTop();
		IndexSort sourceRecentLocation=findSourceRecentLocation();
		indexSortList.add(sourceAll);
		indexSortList.add(sourceRecent);
		indexSortList.add(sourceTop);
		indexSortList.add(sourceRecentLocation);
		
		modelMap.put("errno", 0);
		modelMap.put("indexSortList", indexSortList);
		
		return modelMap;
	}
	//查询全部商品信息
	public IndexSort findSourceAll() {
		
		//查询全部信息
		List<IndexSource> sourceList=sourceService.findSourceAll();
		
		IndexSort indexSort=new IndexSort();
		indexSort.setId(1);
		indexSort.setName("全部");
		if(!sourceList.isEmpty() || sourceList.size()>0) {
			indexSort.setIshaveChild(true);
		}else {
			indexSort.setIshaveChild(false);
		}
		indexSort.setSourceList(sourceList);
		return indexSort;
	} 
	//查询最新商品信息 按照时间查询
//	@RequestMapping("/sourceRecent")
//	@ResponseBody
	public IndexSort findSourceRecent() {
		String dateString=ResourceBundle.getBundle("properties/date").getString("date");
		Integer d=Integer.parseInt(dateString);
		System.out.println(dateString);
		Date date=new Date(System.currentTimeMillis() - d*24*60*60*1000);	
		List<IndexSource> sourceList=sourceService.findSourceRecent(date);
		
		IndexSort indexSort=new IndexSort();
		indexSort.setId(2);
		indexSort.setName("最新发布");
		if(!sourceList.isEmpty() || sourceList.size()>0) {
			indexSort.setIshaveChild(true);
		}else {
			indexSort.setIshaveChild(false);
		}
		indexSort.setSourceList(sourceList);
		return indexSort;
	}
	//强力推荐  这个是置顶商品
//	@RequestMapping("/sourceTop")
//	@ResponseBody
	public IndexSort findSourceTop() {
		List<IndexSource> sourceList=sourceService.findSourceTop();
		
		IndexSort indexSort=new IndexSort();
		indexSort.setId(3);
		indexSort.setName("强力推荐");
		if(!sourceList.isEmpty() || sourceList.size()>0) {
			indexSort.setIshaveChild(true);
		}else {
			indexSort.setIshaveChild(false);
		}
		indexSort.setSourceList(sourceList);
		return indexSort;
		
	}
	
	public IndexSort findSourceRecentLocation() {
//		List<IndexSource> sourceList=sourceService.findSourceTop();
//		
		IndexSort indexSort=new IndexSort();
		indexSort.setId(4);
		indexSort.setName("离我最近");
//		if(!sourceList.isEmpty() || sourceList.size()>0) {
//			indexSort.setIshaveChild(true);
//		}else {
//			indexSort.setIshaveChild(false);
//		}
//		indexSort.setSourceList(sourceList);
		return indexSort;
		
	}
	
	/**
	 * 按照二级类别进行查看
	 */
	@RequestMapping(value="/findSourceByCategory",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> findSourceByCategory(
			@RequestParam("categoryId")Integer categoryId){
		Map<String, Object> modelMap = new HashMap<String,Object>();
		List<IndexSource> categorySourceList=sourceService.findSourceByCategory(categoryId);
		modelMap.put("errno",0);//返回0代表成功
		modelMap.put("categorySourceList", categorySourceList);
		return modelMap;
	}
	
	/**
	 * 前端既是展示二级分类的那个页面，以及点击后展示详细信息
	 * 按照一级类别查找二级类别中包含的所有信息
	 */
	@RequestMapping(value="/findSourceBySort",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> findSourceBySort(
			@RequestParam("sortId")Integer sortId){
		Map<String, Object> modelMap = new HashMap<String,Object>();
		List<CategorySourceVO> categorySourceVOList=new ArrayList<>();
		List<Category> categoryList=assortmentService.findCategotyBySortId(sortId);
		if(!categoryList.isEmpty() || categoryList.size()>0) {
			for (Category category : categoryList) {
				CategorySourceVO categorySourceVO = new CategorySourceVO();
				categorySourceVO.setCategoryId(category.getId());
				categorySourceVO.setCategotyName(category.getCategory_name());
				List<IndexSource> categorySourceList=sourceService.findSourceByCategory(category.getId());
				if(!categorySourceList.isEmpty() || categorySourceList.size()>0) {
					categorySourceVO.setIshaveChild(true);
					
				}else {
					categorySourceVO.setIshaveChild(false);
				}
				categorySourceVO.setCategorySourceList(categorySourceList);
				categorySourceVOList.add(categorySourceVO);
			}
		}
		modelMap.put("errno",0);//返回0代表成功
		modelMap.put("categorySourceVOList", categorySourceVOList);
		return modelMap;
	}
	
	
}
